<?php
/*
 * 前台激活码控制器
 */
class ActivationkeyController extends BasicController {
    
    private function init(){
        Yaf_Registry::get('tokenPlugin')->checkAuth(true);
    }

    /*
    * 获取激活码价格 Api
    */
    public function priceAction(){
        $params = $this->getPost();
        
        $grade = isset($params['grade']) ? $params['grade'] : 0;
        $type = isset($params['type']) ? $params['type'] : 0;
        
        $return['code'] = "0";
        $return['msg'] = '操作成功！';
        $return['data'] = [];
        
        if(!$grade || !$type){//没有传递要查询的参数
            $return['code'] = "1";
            $return['msg'] = '参数错误';
            
            echo json_encode($return);
            return false;
        }
        
        $ActivationkeyPriceModel = Helper::load('Activationkey_price');
        
        $info = $ActivationkeyPriceModel->Where(['ap_grade' => $grade, 'ap_type' => $type, 'is_valid' => 1])->SelectOne();
        
        if(!$info){//找不到 / 无效
            $return['code'] = "2";
            $return['msg'] = '没有对应的价格设置，请联系客服';
            
            echo json_encode($return);
            return false;
        }
        
        $return['data'] = [
            'price'   => $info['ap_price']//激活码
        ];
        
        echo json_encode($return);
        return false;
    }
    
    /*
    * 激活码查询 Api
    */
    public function getinfoAction(){
        $params = $this->getPost();
        
        $key = isset($params['key']) ? $params['key'] : '';
        
        $return['code'] = "0";
        $return['msg'] = '操作成功！';
        $return['data'] = [];
        
        if(!$key){//没有传递要查询的key值
            $return['code'] = "1";
            $return['msg'] = '参数错误';
            
            echo json_encode($return);
            return false;
        }
        
        $ActivationkeyModel = Helper::load('Activationkey');
        
        $info = $ActivationkeyModel->Where(['a_key' => $key, 'is_valid' => 1])->SelectOne();
        
        if(!$info){//找不到 / 无效
            $return['code'] = "2";
            $return['msg'] = '无效的激活码';
            
            echo json_encode($return);
            return false;
        }
        
        $cateModel = Helper::load('Cate');
        
        //激活码时长分类
        $aTypeArr = $cateModel::$enTypeArr;
        //激活码状态分类
        $statusArr = $cateModel::$enStatusArr;
        
        $return['data'] = [
            'key'   => $key,//激活码
            'timetype'  => $aTypeArr[$info['a_type']],//激活码分类 1.一个月 2.三个月 3.半年 4.一年
            'status'=> $statusArr[$info['a_status']]//激活码状态 已激活 未激活
        ];
        
        echo json_encode($return);
        return false;
    }
    
    /*
    * 激活码激活 Api
    */
    public function activationAction(){
        $params = $this->getPost();
        
        $key = isset($params['key']) ? $params['key'] : '';
        $user_id = G_USER_ID;
        
        $return['code'] = "1";
        $return['msg'] = '操作失败！';
        $return['data'] = [];
        
        if(!$key || !$user_id){//没有传递要查询的key值
            $return['code'] = "2";
            $return['msg'] = '参数错误';
            
            echo json_encode($return);
            return false;
        }
        
        $UserDetailModel = Helper::load('User_detail');
        $ClassMainModel = Helper::load('Class_main');
        $ActivationkeyModel = Helper::load('Activationkey');
        $ActivationkeyUseModel = Helper::load('Activationkey_use');
        
        $info = $ActivationkeyModel->Where(['a_key' => $key, 'is_valid' => 1])->SelectOne();
        
        if(!$info || $info['a_status']){//找不到 / 无效 / 已激活
            $return['code'] = "3";
            $return['msg'] = '无效的激活码';
            
            echo json_encode($return);
            return false;
        }
        
        if($info['a_type'] == 1){//一个月
            $x = 1;
        }
        elseif($info['a_type'] == 2){//三个月
            $x = 3;
        }
        elseif($info['a_type'] == 3){//半年
            $x = 6;
        }
        else{//一年
            $x = 12;
        }
        
        //获取当前用户的年级
        $userInfo = $UserDetailModel->Where(['user_id' => $user_id])->SelectOne();
        
        if(!isset($userInfo['class_id'])){//如果该用户没有班级编号
            $return['code'] = "4";
            $return['msg'] = '用户没有绑定班级编号';
            
            echo json_encode($return);
            return false;
        }
        
        //根据班级编号获取对应的年级编号
        $classInfo = $ClassMainModel->Where(['class_id' => $userInfo['class_id']])->SelectOne();
        
        if(!isset($classInfo['grade_num'])){//如果该班级没有对应的年级编号
            $return['code'] = "5";
            $return['msg'] = '未找到对应用户的年级编号';
            
            echo json_encode($return);
            return false;
        }
        
        $grade = $classInfo['grade_num'];//用户当前的年级编号
        $endTime = strtotime(' + '.$x.' month', CUR_TIMESTAMP);
        
        $compTime = strtotime(date('Y-09-01 00:00:00'));
        
        $c = false;
        //此处作对比，如果激活码有效期的截止时间大于对比时间的话，那么说明多出来的部分要转化为下一学期
        if(($endTime > $compTime) && ($grade != 12)){
            
            $uuData['au_key'] = $key;
            $uuData['au_user_id'] = $user_id;
            $uuData['ctime'] = CUR_TIMESTAMP;
            $uuData['mtime'] = CUR_TIMESTAMP;
            
            $uuData['au_grade'] = $grade;
            $uuData['au_stime'] = CUR_TIMESTAMP;
            $uuData['au_etime'] = $compTime;
            $a = $ActivationkeyUseModel->Insert($uuData);
            
            $uuData['au_grade'] = $grade + 1;
            $uuData['au_stime'] = $compTime;
            $uuData['au_etime'] = $endTime;
            $b = $ActivationkeyUseModel->Insert($uuData);
            
            if($a && $b){
                $c = true;
            }
            
        }
        else{//如果不大于的话说明只激活一学期
            $uuData['au_key'] = $key;
            $uuData['au_grade'] = $grade;
            $uuData['au_stime'] = CUR_TIMESTAMP;
            $uuData['au_etime'] = $endTime;
            $uuData['au_user_id'] = $user_id;
            $uuData['ctime'] = CUR_TIMESTAMP;
            $uuData['mtime'] = CUR_TIMESTAMP;
            $c = $ActivationkeyUseModel->Insert($uuData);
        }
        
        if($c){
            $return['code'] = "0";
            $return['msg'] = '操作成功！';
            
            $uData['a_status'] = 1;//激活状态置为已激活
            $uData['mtime'] = CUR_TIMESTAMP;
            $ActivationkeyModel->Where(['a_key' => $key])->UpdateOne($uData);
        }
        
        echo json_encode($return);
        return false;
    }
    
}